package org.jiamandan.com.strategy.impl.car.listener;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jiamandan.com.constant.YxConstant;
import org.jiamandan.com.dto.YXBasicDto;
import org.jiamandan.com.entity.ChargeRule;
import org.jiamandan.com.entity.RechargeGun;
import org.jiamandan.com.entity.TimeModel;
import org.jiamandan.com.service.IRechargeGunService;
import org.jiamandan.com.strategy.impl.car.IYXContextListener;
import org.jiamandan.com.utils.CommonUtil;
import org.jiamandan.com.utils.YxObjectUtil;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * 充电桩计费模型请求
 * @version Revision 1.0.1
 * @版权：版权所有 (c) 2021
 * @author:Lee
 * @email:honvy.lee@qq.com
 * @see:
 * @创建日期：2023-03-23 15:21
 * @功能说明：
 * @begin
 * @修改记录:
 * @修改后版本 修改人        修改内容
 * @2023-03-23 15:21	         Lee        	创建
 * @end
 */
@Service(value = YxConstant.Service.CMD_9)
@AllArgsConstructor
@Slf4j
public class YxCalculateModeListener implements IYXContextListener {

    private static final int[] number = {14, 4};
    private IRechargeGunService iRechargeGunService;

    @Override
    public void doExecute(YXBasicDto dto, Channel client) throws Exception {
//        String pileNo = CommonUtil.removeZero(StringUtils.substring(dto.getData(), 0, number[0]),false);
        String pileNo = StringUtils.substring(dto.getData(), 0, number[0]);
        StringBuilder builder = new StringBuilder();
        builder.append(StringUtils.rightPad(pileNo, 14, "0"));
        builder.append("0100");
        List<TimeModel> timeModelList = new LinkedList<>();
        List<ChargeRule> chargeRules = new LinkedList<>();
        String payload = yxIssueModel(chargeRules, timeModelList);
        builder.append(payload);
        String replyMsg = YxObjectUtil.replyMessage(builder.toString(), dto.getSequence(), YxConstant.Cmd.CMD_0A);
        log.info("JMD-YX计费模型请求应答(CMD = 0A)：" + replyMsg);
        ByteBuf buf = Unpooled.buffer(replyMsg.getBytes().length);
        buf.writeBytes(CommonUtil.hexToByteArray(replyMsg));
        ChannelFuture future = client.writeAndFlush(buf);
    }

    private static String yxIssueModel(List<ChargeRule> chargeRules, List<TimeModel> timeModelList) {
        StringBuilder payload = new StringBuilder();
        StringBuilder rulesb = new StringBuilder();
        StringBuilder timesb = new StringBuilder();
        for (int j = 0, length = chargeRules.size(); j < length; j++) {
            BigDecimal electricFee = BigDecimal.valueOf(chargeRules.get(j).getElectricFee()).multiply(BigDecimal.valueOf(100000));
            String hexElectricFee = CommonUtil.reverseHex(StringUtils.leftPad(Integer.toHexString(electricFee.intValue()), 8, "0"));
            BigDecimal serviceFee = BigDecimal.valueOf(chargeRules.get(j).getServiceFee()).multiply(BigDecimal.valueOf(100000));
            String hexServiceFee = CommonUtil.reverseHex(StringUtils.leftPad(Integer.toHexString(serviceFee.intValue()), 8, "0"));
            rulesb.append(hexElectricFee).append(hexServiceFee);
        }
        for (int i = 0, length = timeModelList.size(); i < length; i++) {
            String dictCode = StringUtils.leftPad(StrUtil.toString(timeModelList.get(i).getDictCode()), 2, "0");
            timesb.append(dictCode);
        }
        payload.append(rulesb);
        payload.append("00");
        payload.append(timesb);
        return payload.toString();
    }
}